package model

import (
	"video/videorpc/videorpc"

	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	Account   string `gorm:"type:char(11);not null;"`
	Password  string `gorm:"type:char(32);not null;"`
	NickName  string `gorm:"type:varchar(30);not null;"`
	HeadImage string `gorm:"type:varchar(500);not null;"`
}

// 视频表
type Video struct {
	gorm.Model
	Title       string `gorm:"type:varchar(50);not null;"`
	VideoClass  string `gorm:"type:varchar(30);not null;"`
	Image       string `gorm:"type:varchar(500);not null;"`
	Info        string `gorm:"type:varchar(300);not null;"`
	UserId      int64  `gorm:"type:int(11);not null;"`
	VideoStatus int64  `gorm:"type:int(11);not null;"`
}

func (v *Video) VideoList(db *gorm.DB, list []*videorpc.VideoList, in *videorpc.VideoListRequest) ([]*videorpc.VideoList, error) {
	err2 := db.Debug().Model(&Video{}).
		Select("videos .* , videos.id , videos.title , " +
			"videos.video_class , videos.image , videos.info , " +
			"videos.user_id , videos.video_status ,users.head_image as head_image ," +
			" users.nick_name as nick_name").
		Joins("left join users on users.id = videos.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list).Error

	return list, err2
}

// 评论表
type Comment struct {
	gorm.Model
	VideoId int64  `gorm:"type:int(11);not null;"`
	UserId  int64  `gorm:"type:int(11);not null;"`
	Content string `gorm:"type:varchar(500);not null;"`
}

func (c *Comment) CommentList(db *gorm.DB, list []*videorpc.CommentList, in *videorpc.CommentListRequest) ([]*videorpc.CommentList, error) {
	err2 := db.Debug().Model(&Comment{}).
		Select("comments .* , users.nick_name as user_nick_name").
		Joins("left join users on users.id = comments.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list).Error
	return list, err2
}

// 收藏表
type Collection struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null;"`
	VideoId int64 `gorm:"type:int(11);not null;"`
}

func (c *Collection) CollectionList(db *gorm.DB, list []*videorpc.CollectionList, in *videorpc.CollectionListRequest) ([]*videorpc.CollectionList, error) {
	err2 := db.Debug().Model(&Collection{}).
		Select("collections .* , videos.image as video_image , videos.title as video_title , users.nick_name as user_nick_name").
		Joins("left join videos on videos.id = collections.video_id").
		Joins("left join users on users.id = collections.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list).Error
	return list, err2
}

// 话题表
type Topic struct {
	gorm.Model
	TopicName  string `gorm:"type:varchar(30);not null;"`
	TopicIntro string `gorm:"type:varchar(500);not null;"`
}
